package com.itheima.company.controller;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import com.itheima.company.pojo.Company;
import com.itheima.company.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author IntelliJ IDEA
 * @date 2020/10/18  10:26
 */
@CrossOrigin
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    /**
     * 测试fegin
     */
    @GetMapping("/test/fegin/hellofegin")
    public String hello(){
        return "hello fegin";
    }

    /**
     * 根据id查询企业
     */
    @GetMapping("/{id}")
    public Result findCompanyById(@PathVariable("id") String id){
        return new Result(ResultCode.SUCCESS,companyService.findCompanyById(id));
    }

    /**
     * 根据id删除企业
     */
    @DeleteMapping("/{id}")
    public Result deleteCompanyById(@PathVariable("id") String id){
        Integer rows = companyService.deleteCompanyById(id);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id修改企业
     */
    @PutMapping
    public Result updateCompanyById(@RequestBody Company company){
        Integer rows = companyService.updateCompanyById(company);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 新增企业
     */
    @PostMapping("/add")
    public Result addCompany(@RequestBody Company company){
        Integer rows = companyService.addCompany(company);
        if(rows<1){
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     *  查询所有的企业
     */
    @GetMapping
    public Result findAllCompanys(){
        return new Result(ResultCode.SUCCESS,companyService.findCompanys());
    }
}
